// #ifdef _WIN32
// 	#include <unistd.h>
// #endif
#include "timer.h"

Timer* Timer::Instance () 
{
	static Timer t;
	return &t; // _instance isn't needed in this case
}

Timer::Timer()
{
	// init mutex

	pthread_mutex_init (&fps_mutex, NULL);
	// calc lasttime for first time
	sdl_lasttime = SDL_GetTicks();
	sdl_firsttime = sdl_lasttime;

	dispcounter	= 0;
	dispsum		= 0.0f;
}

void Timer::mark()
{
	// get now
	sdl_now = SDL_GetTicks();
// 	cerr << SDL_GetTicks() << endl;

// 	cerr << "timer updated from " << sdl_lasttime << " to " << sdl_now << endl;

	// calc diff between now and lasttime
	elapsed = sdl_now - sdl_lasttime;

	if ( elapsed > 0 )
	{
		bullet_ms = 1000.f / elapsed;
		// fps help
		dispcounter++;
		dispsum += elapsed;
	}
	else
		bullet_ms = 0;

	sdl_lasttime = sdl_now;

}
